package com.gasolinator.a.app.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.gasolinator.a.GasWalkerActivity;
import com.gasolinator.a.R;
import com.gasolinator.a.app.GasolinatorStation;
import com.gasolinator.a.app.activity.base.GasolinatorBaseActivity;
import com.gasolinator.a.app.helper.NumberFormats;
import com.gasolinator.a.augmented_reality.data.GasolinatorDataSource;
import com.gasolinator.a.augmented_reality.ui.GasolinatorMarker;
import com.gasolinator.a.augmented_reality.ui.Marker;

import java.util.List;

public class GasolinatorStationDetailActivity extends GasolinatorBaseActivity implements CompoundButton.OnCheckedChangeListener
{
    boolean fMap = false;
    GasolinatorStation gasolinatorStation;
    private int selectedGasolinatorStation;
    private List<Marker> gasolinatorMarkers;



    public void onCheckedChanged(CompoundButton paramCompoundButton, boolean paramBoolean)
    {
        gasolinatorStation.setFavorite(paramBoolean);
        gasolinatorPreferences.handleFavorites(gasolinatorStation.getGasStationId(), paramBoolean);
    }

    public void onCreate(Bundle paramBundle)
    {
        super.onCreate(paramBundle);
        WindowManager windowManager = getWindowManager();
        if (windowManager == null ) {
            setContentView(R.layout.gasolinator_detail);
            fMap = paramBundle.getBoolean("m");
            selectedGasolinatorStation = paramBundle.getInt("selected");
        }
        else {
            int orientation = windowManager.getDefaultDisplay().getOrientation();
            if ((orientation == 1) || (orientation == 3)) {
                setContentView(R.layout.gasolinator_detail_h);
            }
            else
            {
                setContentView(R.layout.gasolinator_detail);
            }

            Bundle bundle = getIntent().getExtras();
            fMap = bundle.getBoolean("m");
            selectedGasolinatorStation = bundle.getInt("selected");

            GasolinatorDataSource gasolinator = GasolinatorDataSource.getInstance(getApplicationContext());
            gasolinatorMarkers = gasolinator.getGasolinatorMarkers();
            gasolinatorStation = ((GasolinatorMarker)gasolinatorMarkers.get(selectedGasolinatorStation)).getStation();
        }
    }



    public void onNavigateAction(View paramView)
    {

        StringBuilder builder = new StringBuilder("google.navigation:q=");
        double latitude = gasolinatorStation.getLatitude();
        builder.append(latitude).append(",");
        double longitude = gasolinatorStation.getLongitude();
        builder.append(longitude);

        Uri uri = Uri.parse(builder.toString());
        Intent intent = new Intent("android.intent.action.VIEW", uri);
        startActivity(intent);
    }

    protected void onPause()
    {
        super.onPause();
    }


    public void onMapAction(View paramView)
    {
        if (this.fMap)
        {
            finish();
            return;
        }
        Context localContext = getApplicationContext();
        Intent localIntent = new Intent(localContext, GasWalkerActivity.class);
        startActivity(localIntent);
        finish();
    }

    protected void onResume()
    {
        super.onResume();


        ImageView imgViewBrandLogo = (ImageView)findViewById(R.id.BrandLogo);
        imgViewBrandLogo.setImageResource(gasolinatorStation.getImageResource());

        TextView textViewName = (TextView)findViewById(R.id.Name);
        textViewName.setText(gasolinatorStation.getName());

        TextView textViewAddress = (TextView)findViewById(R.id.Address);
        textViewAddress.setText(gasolinatorStation.getAddress());

        TextView textViewCity = (TextView)findViewById(R.id.City);
        textViewCity.setText(gasolinatorStation.getCity());

        TextView textViewProvince = (TextView)findViewById(R.id.Province);
        textViewProvince.setText(gasolinatorStation.getProvince());

        TextView textViewTimeTable = (TextView)findViewById(R.id.Timetable);
        textViewTimeTable.setText(gasolinatorStation.getTimetable());

        TextView textViewDateUpdated = (TextView)findViewById(R.id.DateUpdated);
        textViewDateUpdated.setText(gasolinatorStation.getDateUpdated());

        TextView textViewDistance = (TextView)findViewById(R.id.Distance);
        String distance = String.valueOf(NumberFormats.distanceFormat(gasolinatorStation.getDistance()));
        textViewDistance.setText(distance);

        TextView textViewPrice = (TextView)findViewById(R.id.Price);
        String price = String.valueOf(NumberFormats.priceFormat(Double.valueOf(gasolinatorStation.getPrice(combustible))));
        textViewPrice.setText(price);

        CheckBox checkBoxFavorite = (CheckBox)findViewById(R.id.Favorite);
        //TODO: manage favorites
        boolean isFavorite = gasolinatorPreferences.getIsFavorite(gasolinatorStation.getGasStationId());
        gasolinatorStation.setFavorite(isFavorite);
        checkBoxFavorite.setChecked(isFavorite);

        checkBoxFavorite.setOnCheckedChangeListener(this);

    }
}